//
//  XMGNewFeatureViewController.m
//  小码哥彩票
//
//  Created by 徐悟源 on 16/3/8.
//  Copyright (c) 2016年 xiaomage. All rights reserved.
//

#import "XMGNewFeatureViewController.h"
#import "XMGCollectionViewCell.h"
#import "UIView+Frame.h"


// 展示图片的控件应该添加到collectionView上
@interface XMGNewFeatureViewController ()

@property (nonatomic , assign) CGFloat lastOffsetX;
@property (nonatomic , weak) UIImageView *guideView;
@property (nonatomic , weak) UIImageView *guideLargeView;
@property (nonatomic , weak) UIImageView *guideSmallView;

@end

@implementation XMGNewFeatureViewController

static NSString *reuseIdentifier = @"Cell";

- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    
    layout.minimumInteritemSpacing = 0;
    
    layout.minimumLineSpacing = 0;
    
    return  [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.bounces = NO;
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    self.collectionView.pagingEnabled = YES;
    
    
    [self.collectionView registerClass:[XMGCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self setUpAllChildView];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete method implementation -- Return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete method implementation -- Return the number of items in the section
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    NSString *imageName = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item + 1];
    
    [cell setImage:[UIImage imageNamed:imageName]];
    
    [cell setUpIndexPath:indexPath count:4];
    
    return cell;
}

#pragma mark - 添加所有子控件

- (void)setUpAllChildView
{
    // guide
    UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    _guideView = guide;
    guide.centerX = self.view.centerX;
    
    [self.collectionView addSubview:guide];
    
    // guideLine
    UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    guideLine.x -= 170;
    [self.collectionView addSubview:guideLine];
    
    // largeText
    UIImageView *largeText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    largeText.centerX = self.view.centerX;
    largeText.centerY = self.view.height * 0.7;
    _guideLargeView = largeText;
    [self.collectionView addSubview:largeText];
    
    // smallText
    UIImageView *smallText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    smallText.centerX = self.view.centerX;
    smallText.centerY = self.view.height * 0.8;
    _guideSmallView = smallText;
    [self.collectionView addSubview:smallText];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 获取当前x的偏移量
    CGFloat curOffsetX = scrollView.contentOffset.x;
    
    // 获取差值
    CGFloat delta = curOffsetX - _lastOffsetX;
    
    _guideView.x += 2*delta;
    _guideLargeView.x += 2*delta;
    _guideSmallView.x += 2*delta;
    
    [UIView animateWithDuration:0.25 animations:^{
        _guideView.x -= delta;
        _guideLargeView.x -= delta;
        _guideSmallView.x -= delta;
    }];
    
    int page = curOffsetX / self.view.width + 1;
    
    // 修改控件内容
    _guideView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%d",page]];
    _lastOffsetX = curOffsetX;
}



@end
